'use strict'

let { Users, MovieEvaluationForm, ModelEvaluationForm, CityEvaluationForm, CarEvaluationForm, StaticBackEvaluationForm ,AdminUser} = require('../models');

let auth = require('../session/index');
//登录界面
let login_fn = async (ctx, next) => {
    ctx.render('login.html')
}

//用户页面
let index_fn = async (ctx, next) => {

    //遍历电影评测

    let movie = JSON.parse(JSON.stringify(await MovieEvaluationForm.findAll({
        //循环ID
        //Id最大值的一行
        //赋值给movie
        order:[
            ["id","desc"]
        ]

    })));


    //遍历城市评测
    let city = JSON.parse(JSON.stringify(await CityEvaluationForm.findAll({
        order:[
            ["id","desc"]
        ]
    })));

    //遍历模特评测
    let models = JSON.parse(JSON.stringify(await ModelEvaluationForm.findAll({

        order:[
            ["id","desc"]
        ]
    })));

    //遍历汽车评测
    let cars = JSON.parse(JSON.stringify(await CarEvaluationForm.findAll({
        order:[
            ["id","desc"]
        ]

    })));

    //遍历其他评测
    let staticback = JSON.parse(JSON.stringify(await StaticBackEvaluationForm.findAll({
        order:[
            ["id","desc"]
        ]

    })));

    let obj = {
        one: movie,
        tow: city,
        three: models,
        four: cars,
        five: staticback
    }


    ctx.render('index.html', obj)
}


//客户端页面
let backIndex = async (ctx, next) => {
    ctx.render('backIndex.html')
}

//用户注册界面
let registered_fn = async (ctx, next) => {
    ctx.render('registered.html')
}

//客户端注册界面
let backRegistered_fn = async (ctx, next) => {
    ctx.render('backRegistered.html')
}

//电影评测界面
let movieReviews_fn = async (ctx, next) => {
    ctx.render('movieReviews.html')
}

//城市评测界面
let softwareEvaluation_fn = async (ctx, next) => {
    ctx.render('softwareEvaluation.html')
}

//模特评测界面
let modelReviews_fn = async (ctx, next) => {
    ctx.render('modelReviews.html')
}

//汽车评测界面
let carRvaluation_fn = async (ctx, next) => {
    ctx.render('carRvaluation.html')
}


//实现登录跳转
let loginDone_fn = async (ctx, next) => {
    let usersText = ctx.request.body.account || '';
    let pwdText = ctx.request.body.pwd || '';

    let res;
    let u1 = await Users.findOne({
        where: {
            username: usersText,
            password: pwdText
        }
    }).then((row) => {
        let user = JSON.stringify(row);
        let u2 = JSON.parse(user);
        if (user !== 'null') {
            ctx.session.userId = row.id;
            ctx.session.username = row.username;
            res = { code: 200, msg: '登录成功' };
        } else {
            res = { code: 1000, msg: '账号或者密码错误请重试' };
        }
    });
    ctx.body = res;
}

//实现注册
let registerDone_fn = async (ctx, next) => {
    let username = ctx.request.body.reister_user || '';
    let password = ctx.request.body.reister_pwd || '';
    let confirmpassword = ctx.request.body.reister_conpwd || '';
    if (username.length > 0 && password.length > 0 && password === confirmpassword) {
        let userDemo = await Users.findOne({ where: { username: username } })
        let res = '';

        if (userDemo) {
            res = { code: 1000, msg: '当前用户名已经注册，请确认后重试' };
        } else {
            let u1 = Users.create({
                username: username,
                password: password
            });
            res = { code: 200, msg: '注册成功' };
        }

        ctx.body = JSON.stringify(res);

    } else {
        alert('用户名或密码不能为空；并且两次密码应该相同');
    }
}
//电影评测表
let movieData_fn = async (ctx, next) => {
    let movieone = ctx.request.body.movieone || '';
    let movietwo = ctx.request.body.movietwo || '';
    let moviethre = ctx.request.body.moviethre || '';
    let moviefour = ctx.request.body.moviefour || '';
    let moviefive = ctx.request.body.moviefive || '';
    let movieData = await MovieEvaluationForm.create({
        HeadlineOne: movieone,
        HeadlineTwo: movietwo,
        HeadlineThree: moviethre,
        HeadlineFour: moviefour,
        HeadlineFive: moviefive
    });

    let data = JSON.stringify(movieData);
    ctx.body = { code: 200, data, msg: '发布成功' };
}
//城市评测表
let softwareData_fn = async (ctx, next) => {
    let softwareone = ctx.request.body.softwareone || '';
    let softwaretwo = ctx.request.body.softwaretow || '';
    let softwarethre = ctx.request.body.softwarethre || '';
    let softwarefour = ctx.request.body.softwarefour || '';
    let softwarefive = ctx.request.body.softwarefive || '';
    let softwareData = await CityEvaluationForm.create({
        HeadlineOne: softwareone,
        HeadlineTwo: softwaretwo,
        HeadlineThree: softwarethre,
        HeadlineFour: softwarefour,
        HeadlineFive: softwarefive
    });

    let data = JSON.parse(JSON.stringify(softwareData));
    ctx.body = { code: 200, data, msg: '发布成功' };
}
//模特评测表
let modelData_fn = async (ctx, next) => {

    let modelone = ctx.request.body.modelone || '';
    let modeltwo = ctx.request.body.modeltow || '';
    let modelthre = ctx.request.body.modelthre || '';
    let modelfour = ctx.request.body.modelfour || '';
    let modelfive = ctx.request.body.modelfive || '';

    let modelData = await ModelEvaluationForm.create({
        HeadlineOne: modelone,
        HeadlineTwo: modeltwo,
        HeadlineThree: modelthre,
        HeadlineFour: modelfour,
        HeadlineFive: modelfive
    });

    let data = JSON.stringify(modelData);
    ctx.body = { code: 200, data, msg: '发布成功' };
}

//汽车评测表
let carData_fn = async (ctx, next) => {

    let carone = ctx.request.body.carone || '';
    let cartwo = ctx.request.body.cartwo || '';
    let carthre = ctx.request.body.carthre || '';
    let carfour = ctx.request.body.carfour || '';
    let carfive = ctx.request.body.carfive || '';

    let modelData = await CarEvaluationForm.create({
        HeadlineOne: carone,
        HeadlineTwo: cartwo,
        HeadlineThree: carthre,
        HeadlineFour: carfour,
        HeadlineFive: carfive
    });

    let data = JSON.stringify(modelData);
    ctx.body = { code: 200, data, msg: '发布成功' };
}
//其他评测表
let staticbackData_fn = async (ctx, next) => {
    let staticbackname = ctx.request.body.staticbackname || '';
    let staticbackone = ctx.request.body.staticbackone || '';
    let staticbacktwo = ctx.request.body.staticbacktwo || '';
    let staticbackthre = ctx.request.body.staticbackthre || '';
    let staticbackfour = ctx.request.body.staticbackfour || '';
    let staticbackfive = ctx.request.body.staticbackfive || '';

    let modelData = await StaticBackEvaluationForm.create({
        Headlinename: staticbackname,
        HeadlineOne: staticbackone,
        HeadlineTwo: staticbacktwo,
        HeadlineThree: staticbackthre,
        HeadlineFour: staticbackfour,
        HeadlineFive: staticbackfive
    });

    let data = JSON.stringify(modelData);
    ctx.body = { code: 200, data, msg: '发布成功' };
}

//站主登录界面
let backlogin_fn = async(ctx,next)=>{
    ctx.render('backlogin.html')
}

//站主注册
let backregister_fn = async(ctx,next)=>{
    let username = ctx.request.body.back_user || '';
    let password = ctx.request.body.back_pwd || '';
    let confirmpassword = ctx.request.body.back_conpwd || '';
    if (username.length > 0 && password.length > 0 && password === confirmpassword) {
        let userDemo = await AdminUser.findOne({ where: { username: username } })
        let res = '';

        if (userDemo) {
            res = { code: 1000, msg: '当前用户名已经注册，请确认后重试' };
        } else {
            let u1 = Users.create({
                username: username,
                password: password
            });
            res = { code: 200, msg: '注册成功' };
        }

        ctx.body = JSON.stringify(res);

    } else {
        alert('用户名或密码不能为空；并且两次密码应该相同');
    }
}

//站主登录
let backloginDome_fn = async(ctx,next)=> {
    let accounts = ctx.request.body.accounts || '';
    let passwords = ctx.request.body.PassWords || '';
    let res;
    let u1 = await AdminUser.findOne({
        where: {
            username: accounts,
            password: passwords
        }
    }).then((row) => {
        let user = JSON.stringify(row);
        let u2 = JSON.parse(user);
        if (user !== 'null') {
            ctx.session.useraccount = row.id;
            ctx.session.userpwd = row.username;
            res = { code: 200, msg: '登录成功' };
        } else {
            res = { code: 1000, msg: '账号或者密码错误请重试' };
        }
    });
    ctx.body = res;
}
module.exports = {
    '/': ['get', login_fn],
    '/index': ['get',auth, index_fn],
    '/backIndex': ['get', auth,backIndex],
    '/registered': ['get', registered_fn],
    '/backRegistered': ['get', backRegistered_fn],
    '/movie': ['get', movieReviews_fn],
    '/softWare': ['get', softwareEvaluation_fn],
    '/modelReviews': ['get', modelReviews_fn],
    '/carRvaluation': ['get', carRvaluation_fn],
    '/backlogin':['get',auth,backlogin_fn],
    '/loginDone': ['post', loginDone_fn],
    '/registerDone': ['post', registerDone_fn],
    '/movieEvaluationDone': ['post', movieData_fn],
    '/softwareEvaluationDone': ['post', softwareData_fn],
    '/modelEvaluationDone': ['post', modelData_fn],
    '/carEvaluationDone': ['post', carData_fn],
    '/staticbackEvaluationDone': ['post', staticbackData_fn],
    '/backregisterDone':['post' , backregister_fn],
    '/backloginDome':['post' , backloginDome_fn]
}